package timeutil

import (
	"time"
)

var (
	cst *time.Location
)

const Format1 = "2006-01-02 15:04:05"

const Format2 = "2006-01-02 15:04:05.000"

const Format3 = "20060102150405"

const Format4 = "2006-01-02"

func init() {
	//var err error
	//if cst, err = time.LoadLocation("Asia/Shanghai"); err != nil {
	//	panic(err)
	//}
	// 使用 FixedZone 创建 CST 时区
	cst = time.FixedZone("CST", 8*3600) // UTC+8
	// 默认设置为中国时区
	time.Local = cst
}

// TimeToStr 格式化时间
func TimeToStr() string {
	ts := time.Now()
	return ts.In(cst).Format(Format1)
}

// TimeToStrWithFormat 格式化时间
func TimeToStrWithFormat(datetime time.Time, format string) string {
	return datetime.In(cst).Format(format)
}

// StrToTime 字符串转时间
func StrToTime(format, date string) (time.Time, error) {
	return time.ParseInLocation(format, date, cst)
}

// StrToDateTimeCST 字符串转标准时间
func StrToDateTimeCST(datetime string) (time.Time, error) {
	return time.ParseInLocation(Format1, datetime, cst)
}

// TimeToSeconds 计算时间的秒数
func TimeToSeconds(t time.Time) int {
	return t.Hour()*3600 + t.Minute()*60 + t.Second()
}
